package com.wei.tang.db;

import java.util.ArrayList;
import java.util.List;

import com.wei.tang.bean.CourseBean;
import com.wei.tang.bean.UserBean;
import com.wei.tang.db.DBInfo.CourseTable;
import com.wei.tang.db.DBInfo.UserTable;
import com.wei.tang.util.Tlog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

/**
 * 
 * @author 吴佳明
 *
 */
public class CourseDao extends AndroidTestCase {

	private DBHelper dbHelper = null;
	private SQLiteDatabase db = null;
	private ContentValues values = null;
	String[] columns = { DBInfo.UserTable.USER_NAME };

	public CourseDao(Context context) {
		Tlog.i("进入数据库类");
		dbHelper = new DBHelper(context);
		Tlog.i("数据库  表创建成功");
	}

	/**
	 * 用户注册
	 * 
	 * @param userBean
	 * @return
	 */
	public CourseBean publishCourse(String _classname, String _classdesc, String _classpath) {
		Tlog.i("用户注册");
		CourseBean courseBean = null;
		courseBean = new CourseBean(_classname, _classdesc, _classpath);
		if (insertCourse(courseBean)) {
		} else {
			courseBean = null;
		}
		return courseBean;
	}

	public List<CourseBean> findAllCourse() {
		String sql = " ";
		String[] strs = {};
		return findUserBySql(sql, strs);
	}

	public List<CourseBean> findUserBySql(String sql, String[] strs) {
		db = dbHelper.getReadableDatabase();
		Cursor cur = null;
		List<CourseBean> courseBeans = new ArrayList<CourseBean>();
		try {
			cur = db.query(CourseTable.COURSE_TABLE, null, null, null, null, null, null, null);
			if (cur.moveToNext()) {
				CourseBean courseBean = fillUserBean(cur);
				courseBeans.add(courseBean);
			}
			cur.close();
		} catch (Exception e) {
			Tlog.e("e:" + e.getMessage());
		}
		db.close();
		return courseBeans;
	}

	private CourseBean fillUserBean(Cursor cur) {
		CourseBean courseBean = new CourseBean();
		courseBean.setCourse_id(cur.getString(cur.getColumnIndex(CourseTable.COURSE_ID)));
		courseBean.setCourse_name(cur.getString(cur.getColumnIndex(CourseTable.COURSE_NAME)));
		courseBean.setCourse_url(cur.getString(cur.getColumnIndex(CourseTable.COURSE_URL)));
		courseBean.setCourse_loadtime(cur.getString(cur.getColumnIndex(CourseTable.COURSE_LOADTIME)));
		courseBean.setCourse_userid(cur.getString(cur.getColumnIndex(CourseTable.COURSE_USERID)));
		courseBean.setCourse_username(cur.getString(cur.getColumnIndex(CourseTable.COURSE_USERNAME)));
		courseBean.setCourse_classifyid(cur.getString(cur.getColumnIndex(CourseTable.COURSE_CLASSIFYID)));
		courseBean.setCourse_size(cur.getString(cur.getColumnIndex(CourseTable.COURSE_SIZE)));
		courseBean.setCourse_time(cur.getString(cur.getColumnIndex(CourseTable.COURSE_PLAYTIMES)));
		courseBean.setCourse_playtimes(cur.getString(cur.getColumnIndex(CourseTable.COURSE_PLAYTIMES)));
		courseBean.setCourse_commenttimes(cur.getString(cur.getColumnIndex(CourseTable.COURSE_COMMENTTIMES)));
		courseBean.setCourse_praisetimes(cur.getString(cur.getColumnIndex(CourseTable.COURSE_PRAISETIMES)));
		return courseBean;
	}

	public boolean insertCourse(CourseBean courseBean) {
		Tlog.i("插入用户数据到数据库中");
		db = dbHelper.getReadableDatabase();
		values = fillContentValues(courseBean);
		long rowId = db.insert(DBInfo.CourseTable.COURSE_TABLE, DBInfo.CourseTable.COURSE_NAME, values);
		db.close();
		Tlog.i("是否存成功：" + rowId);
		if (rowId == -1) {
			return false;
		} else {
			return true;
		}
	}

	private ContentValues fillContentValues(CourseBean courseBean) {
		ContentValues tempValues = new ContentValues();
		tempValues.put(DBInfo.CourseTable.COURSE_ID, courseBean.getCourse_id());
		tempValues.put(DBInfo.CourseTable.COURSE_NAME, courseBean.getCourse_name());
		tempValues.put(DBInfo.CourseTable.COURSE_URL, courseBean.getCourse_url());
		tempValues.put(DBInfo.CourseTable.COURSE_LOADTIME, courseBean.getCourse_loadtime());
		tempValues.put(DBInfo.CourseTable.COURSE_USERID, courseBean.getCourse_userid());
		tempValues.put(DBInfo.CourseTable.COURSE_USERNAME, courseBean.getCourse_username());
		tempValues.put(DBInfo.CourseTable.COURSE_CLASSIFYID, courseBean.getCourse_classifyid());
		tempValues.put(DBInfo.CourseTable.COURSE_SIZE, courseBean.getCourse_size());
		tempValues.put(DBInfo.CourseTable.COURSE_TIME, courseBean.getCourse_time());
		tempValues.put(DBInfo.CourseTable.COURSE_PLAYTIMES, courseBean.getCourse_playtimes());
		tempValues.put(DBInfo.CourseTable.COURSE_COMMENTTIMES, courseBean.getCourse_commenttimes());
		tempValues.put(DBInfo.CourseTable.COURSE_PRAISETIMES, courseBean.getCourse_praisetimes());
		return tempValues;
	}
}
